package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class SignUp {
    private JFrame frame;
    private JTextField usernameField;
    private JTextField passwordField;
    private JButton loginButton;

    public SignUp() {
        frame = new JFrame("用户注册");
        frame.setSize(300, 200);
        // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel(new GridLayout(2, 1)); //使用GridLayout布局管理器

        JPanel usernamePanel = new JPanel();
        JLabel usernameLabel = new JLabel("账号:");
        usernameField = new JTextField(20);
        usernamePanel.add(usernameLabel);
        usernamePanel.add(usernameField);
        panel.add(usernamePanel);

        JPanel passwordPanel = new JPanel();
        JLabel passwordLabel = new JLabel("密码:");
        passwordField = new JTextField(20);
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);
        panel.add(passwordPanel);

        loginButton = new JButton("注册");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getText());
                if (validateCredentials(username, password)) {
                    SignUp.addAccount(username,password);
                    frame.dispose(); //验证通过，则关闭窗口
                } else {
                    JOptionPane.showMessageDialog(frame, "用户已存在", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        frame.add(panel, BorderLayout.CENTER);
        frame.add(loginButton, BorderLayout.SOUTH);
    }
    private boolean validateCredentials(String username, String password) {
        //进行账号密码验证的逻辑
        return check(username,password);
    }
    public void show() {
        frame.setVisible(true);
    }
    public static void runSignUp(){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                SignUp program = new SignUp();
                program.show();
            }
        });
    }
    public static boolean check(String id,String password){//用来检查账号的合法性 不能重复
        boolean res=true;
        File file=new File("D:\\java\\zxbcode\\game\\account.txt");
        try(BufferedReader br=new BufferedReader(new FileReader(file))){//先遍历一遍
            String line;
            while((line=br.readLine())!=null){
                String[] words=line.split(" ");
                if(words[0].equals(id)){
                    res=false;
                }
            }
        }catch(IOException e){
            e.printStackTrace();
        }
        return res;
    }
    public static void addAccount(String id,String password){//直接在最后面添加
        try(PrintWriter pw=new PrintWriter(new FileWriter("D:\\java\\zxbcode\\game\\account.txt", true))){ //true代表直接在后面添加
            pw.printf("%s %s\n",id,password);
            pw.flush();//刷新缓冲区
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}